package rs.dalao.syc.doctor.all.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rs.dalao.syc.doctor.all.dao.persist.repository.IDoctorCredentialRepository;
import rs.dalao.syc.doctor.all.pojo.entity.DoctorCredential;
import rs.dalao.syc.doctor.all.pojo.param.DoctorCredentialParam;
import rs.dalao.syc.doctor.all.pojo.vo.DoctorCredentialVO;
import rs.dalao.syc.doctor.all.service.IDoctorCredentialService;
import rs.dalao.syc.doctor.all.service.IDoctorService;

@Service
public class DoctorCredentialServiceImpl implements IDoctorCredentialService {
    @Autowired
    private IDoctorCredentialRepository doctorCredentialRepository;

    //新增或者修改用户的证书信息
    @Override
    public int insert(DoctorCredentialParam doctorCredentialParam,Long id) {
        DoctorCredential doctorCredential = new DoctorCredential();
        doctorCredential.setDoctorId(id);
        BeanUtils.copyProperties(doctorCredentialParam,doctorCredential);
        DoctorCredentialVO doctorCredentialVO = doctorCredentialRepository.selectById(id);
        //如果信息不存在，执行新增
        if(doctorCredentialVO==null){
            int insert = doctorCredentialRepository.insert(doctorCredential);
            //执行新增后，将syc_doctor中的role从3变成4
            int count = doctorCredentialRepository.updateRoleById(id);
            System.out.println(count);
            return insert;
        }
        //如果信息存在修改用户的真实信息
        int count = doctorCredentialRepository.updateById(doctorCredentialParam, id);
        return count;
    }

    //根据医生id查询医生证书信息
    @Override
    public DoctorCredentialVO selectById(Long id) {
        return doctorCredentialRepository.selectById(id);
    }
}
